package com.eascs.workflow.sys.controller;

import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.workflow.auditorReplace.vo.AuditorReVo;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.sys.dao.SysOrgDao;
import com.eascs.workflow.sys.dao.SysUserDao;
import com.eascs.workflow.sys.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.eascs.site.view.HtmlRestResult.popHtmlFragment;

@RestController
public class UserRestController extends BaseRestController {
	@Autowired
	private SysUserDao sysUserDao;
	
	@Autowired
	private SysOrgDao sysOrgDao;

	@RequestMapping(value = "/act/search/employee")
	public BaseRestResult<List<Object[]>> employee(String empName) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(empName)) {
			data = sysUserDao.getLikeEmployeeList(empName);
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	@RequestMapping(value = "/act/search/org")
	public BaseRestResult<List<Object[]>> searchOrg(String orgName) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(orgName)) {
			data = sysOrgDao.getOrgAllLikeName(orgName);
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	@RequestMapping("/delete/user-resources")
	public BaseRestResult<String> deleteUserResources(String userNo)
	{
		sysUserDao.deleteUserResources(userNo);
		return BaseRestResult.info("删除成功").andRefresh();
	}

	@RequestMapping("/rest/user/detail")
	public HtmlRestResult getUserDetail(EmployeeVo vo) {
		Object[] user = sysUserDao.queryUserEmployeeByUserNo(vo);
		ViewContext.put("vo", user);
		List<ActOrgEntity> proList = sysOrgDao.getOrgProvinceAndHQ();
		ViewContext.put("proList", proList);
		return popHtmlFragment("/user/user_editer");
	}

	@RequestMapping("/rest/employee/save")
	public BaseRestResult<String> assetsSave(EmployeeVo vo) {
		boolean flag = sysUserDao.editorUserStatus(vo);

		if (flag) {
			return BaseRestResult.info("修改成功").andRefresh().andCloseActionDialog();
		} else {
			return BaseRestResult.warn("修改失败").andRefresh().andCloseActionDialog();
		}

	}
	
	
	
	@RequestMapping(value = "/search/post")
	public BaseRestResult<List<Object[]>> employee(EmployeeVo vo) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(vo.getPostName())) {
			data = sysUserDao.getLikePostList(vo);
		}
		return BaseRestResult.buildRestResult(data);
	}
}
